package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.Message;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.service.UserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("userMessage")
@RestController
@Api(tags = "UserMessageController",description = "前台-个人中心-消息列表")
public class UserMessageController {
    @Autowired
    private UserMessageService userMessageService;

    @RequestMapping(value = "getMessageList.do",method = RequestMethod.POST)
    @ApiOperation("个人中心-消息列表")
    public ServerResponse<PageInfo<List<Message>>> getMessageList(HttpServletRequest httpServletRequest,
                                                                  Page page, @ApiParam(value = "状态 0未读 1已读 2全部",required = true)@RequestParam Integer status){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return  userMessageService.getMessageList(page,status);
    }

    /**
     * 删除消息
     * @param id
     * @return
     */
    @RequestMapping(value = "delMessage.do",method = RequestMethod.POST)
    @ApiOperation("个人中心-消息-删除")
    public ServerResponse delMessage(@RequestParam Integer id){

        return userMessageService.delMessage(id);
    }

    /**
     * 标记为已读
     * @return
     */
    @RequestMapping(value = "checkMsg.do",method = RequestMethod.POST)
    @ApiOperation("个人中心-消息-标记为已读")
    public ServerResponse checkMsg(HttpServletRequest httpServletRequest,@RequestParam Long id){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return userMessageService.checkMsg(user.getId(),id);
    }


}
